package com.yanqu.road.server.cmd.activity.lottery;

import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ActivityLotteryProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.lottery.ActivityLotteryActivity;
import com.yanqu.road.server.manager.activity.lottery.CrossActivityLotteryMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C_ACTIVITY_LOTTERY_DRAW_HIDE_BOX, desc = "抽隐藏盲盒")
public class CrossActivityLotteryDrawHideBoxCmd extends CrossCommand {

    int activityId;
    long serverId;
    long userId;

    private boolean canDrawHide = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ActivityLotteryProto.CrossActivityLotteryDrawHideBoxReqMsg reqMsg = ActivityLotteryProto.CrossActivityLotteryDrawHideBoxReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        serverId = reqMsg.getServerId();
        userId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ActivityLotteryActivity activity = CrossActivityLotteryMgr.getLotteryActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
            return;
        }

        canDrawHide = activity.drawHideBox(userId);
    }

    @Override
    public void sendData() {
        ActivityLotteryProto.CrossActivityLotteryDrawHideBoxRespMsg.Builder builder = ActivityLotteryProto.CrossActivityLotteryDrawHideBoxRespMsg.newBuilder();
        builder.setRet(ret);
        builder.setActivityId(activityId);
        if (ret == 0) {
            builder.setIsHide(canDrawHide);
        }
        MessageHelper.sendPacket(serverId, userId, YanQuMessageUtils.buildMessage(GameProtocol.S_ACTIVITY_LOTTERY_DRAW_HIDE_BOX_FROM_CROSS, builder));
    }
}
